﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Lab1
{
    /// <summary>
    /// Interaction logic for QuestionVariant.xaml
    /// </summary>
    public partial class QuestionVariant : Page
    {
        private Guid selectedAnswerId = Guid.Empty;
        private QuestionService qService = new QuestionService();
        private AnswerService aService = new AnswerService();
        public QuestionVariant()
        {
            InitializeComponent();
            DatabaseEntities db = new DatabaseEntities();

            Question question = qService.getById((Guid)Application.Current.Properties["questionId"]);
            QuestionTitle.Content = question.Title;
            Title = question.Title;
            IEnumerable<AnswerVariant> ans = aService.getByQuestion(question.Id);
            answerListBox.DataContext = ans;
            QuestionNumber.Text = UserAnswerService.CurrentNumber.ToString();
        }

        public void NextQuestion(object sender, RoutedEventArgs e)
        {
            try
            {
                AnswerVariant answer = aService.getById(selectedAnswerId);
                UserAnswerService.Add(new UserAnswer
                {
                    AnswerVariantId=answer.Id,
                    Number=UserAnswerService.CurrentNumber,
                    QuestionId = (Guid)Application.Current.Properties["questionId"]
                });
                Guid que = answer.QuestionChildId.GetValueOrDefault(Guid.Empty);
                if (que.Equals(Guid.Empty))
                {
                    this.NavigationService.Navigate(new Result());
                }
                else
                {
                    Question question = qService.getById(answer.QuestionChildId.GetValueOrDefault(Guid.Empty));
                    Application.Current.Properties["questionId"] = answer.QuestionChildId;
                    if (question.Type.Equals("Free"))
                    {
                        this.NavigationService.Navigate(new QuestionFree());
                    }
                    else
                    {
                        this.NavigationService.Navigate(new QuestionVariant());
                    }

                }
            }
            catch
            {
                MessageBox.Show("Вы не выбрали значение!");
            }
        }

        private void SelectAnswer(object sender, RoutedEventArgs e)
        {
            selectedAnswerId = (Guid)(sender as RadioButton).Tag;
        }
    }
}
